#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <linux/netlink.h>



int main(int argc, char *argv[])
{
    int ret;
    struct sockaddr_nl *nl;
    int len;
    char buff[4096] = {0};
    int i;

    bzero(nl, sizeof(struct sockaddr_nl));
    nl->nl_family = AF_NETLINK;
    nl->nl_pid = 0;
    nl->nl_groups = 1;

    int sfd = socket(AF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT);
    if (sfd < 0)
    {
        printf("create socket error\n");
        return -1;
    }

    ret = bind(sfd, (struct sockaddr *)nl, sizeof(struct sockaddr_nl));
    if (ret < 0)
    {
        printf("bind error\n");
        return -1;
    }

    while (1)
    {
        bzero(buff, sizeof(buff));
        len = recv(sfd, buff, 4096, 0);

        for (i = 0; i < len;  i ++)
        {
            if (buff[i] == '\0'){
                buff[i] = '\n';
            }
        }

        printf("%s\n", buff);      
    }
    return 0;
}

